<?xml version="1.0" encoding="UTF-8" ?>
<!--

    Copyright Camunda Services GmbH and/or licensed to Camunda Services GmbH
    under one or more contributor license agreements. See the NOTICE file
    distributed with this work for additional information regarding copyright
    ownership. Camunda licenses this file to you under the Apache License,
    Version 2.0; you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

        http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.

-->
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> 
  
<mapper namespace="org.camunda.bpm.engine.impl.persistence.entity.GroupEntity">

  <!-- GROUP INSERT -->

  <insert id="insertGroup" parameterType="org.camunda.bpm.engine.impl.persistence.entity.GroupEntity">
    insert into ${prefix}ACT_ID_GROUP (ID_, NAME_, TYPE_, REV_)
    values (
      #{id ,jdbcType=VARCHAR},
      #{name ,jdbcType=VARCHAR},
      #{type ,jdbcType=VARCHAR},
      1
    )
  </insert>

  <!-- GROUP UPDATE -->
  
  <update id="updateGroup" parameterType="org.camunda.bpm.engine.impl.persistence.entity.GroupEntity">
    update ${prefix}ACT_ID_GROUP set
      REV_ = #{revisionNext ,jdbcType=INTEGER},
      NAME_ = #{name ,jdbcType=VARCHAR},
      TYPE_ = #{type ,jdbcType=VARCHAR}
    where ID_ = #{id}
       and REV_ = #{revision}
  </update>
  
  <!-- GROUP DELETE -->

  <delete id="deleteGroup" parameterType="org.camunda.bpm.engine.impl.persistence.entity.GroupEntity">
    delete from ${prefix}ACT_ID_GROUP where ID_ = #{id} and REV_ = #{revision} 
  </delete>

  <!-- GROUP RESULTMAP -->

  <resultMap id="groupResultMap" type="org.camunda.bpm.engine.impl.persistence.entity.GroupEntity">
    <id property="id" column="ID_" jdbcType="VARCHAR" />
    <result property="revision" column="REV_" jdbcType="INTEGER" />
    <result property="name" column="NAME_" jdbcType="VARCHAR" />
    <result property="type" column="TYPE_" jdbcType="VARCHAR" />
  </resultMap>
  
  <!-- GROUP SELECT -->

  <select id="selectGroup" parameterType="string" resultMap="groupResultMap">
    select * from ${prefix}ACT_ID_GROUP where ID_ = #{id}
  </select>
    
  <select id="selectGroupsByUserId" parameterType="org.camunda.bpm.engine.impl.db.ListQueryParameterObject" resultMap="groupResultMap">
    select g.*
    from ${prefix}ACT_ID_GROUP g, ${prefix}ACT_ID_MEMBERSHIP membership
    where g.ID_ = membership.GROUP_ID_
      and membership.USER_ID_ = #{parameter}
  </select>

  <select id="selectGroupsByUserIdAndGroupType" parameterType="org.camunda.bpm.engine.impl.db.ListQueryParameterObject" resultMap="groupResultMap">
    select g.*
    from ${prefix}ACT_ID_GROUP g, ${prefix}ACT_ID_MEMBERSHIP membership
    where g.ID_ = membership.GROUP_ID_
      and membership.USER_ID_ = #{parameter.userId}
      <if test="groupType != null">
         and g.TYPE_ = #{parameter.groupType}
      </if>
  </select>
  
  <select id="selectGroupByQueryCriteria" parameterType="org.camunda.bpm.engine.impl.GroupQueryImpl" resultMap="groupResultMap">
  	<include refid="org.camunda.bpm.engine.impl.persistence.entity.Commons.bindOrderBy"/>
    ${limitBefore}
    select ${distinct} RES.* 
    ${limitBetween}
    <include refid="selectGroupByQueryCriteriaSql" />
    ${orderBy}
    ${limitAfter}
  </select>
  
   <select id="selectGroupCountByQueryCriteria" parameterType="org.camunda.bpm.engine.impl.GroupQueryImpl" resultType="long">
    ${countDistinctBeforeStart} RES.ID_ ${countDistinctBeforeEnd}
    <include refid="selectGroupByQueryCriteriaSql" />
    ${countDistinctAfterEnd}
  </select>
  
  <sql id="selectGroupByQueryCriteriaSql">

    from ${prefix}ACT_ID_GROUP RES
    <if test="userId != null">
      inner join ${prefix}ACT_ID_MEMBERSHIP M on RES.ID_ = M.GROUP_ID_
      inner join ${prefix}ACT_ID_USER U on M.USER_ID_ = U.ID_
    </if>
    <if test="tenantId != null">
      inner join ${prefix}ACT_ID_TENANT_MEMBER TM on RES.ID_ = TM.GROUP_ID_
    </if>

    <include refid="org.camunda.bpm.engine.impl.persistence.entity.AuthorizationEntity.authCheckJoin" /> 

    <where>
      <if test="id != null">
        RES.ID_ = #{id}
      </if>
      <if test="ids != null &amp;&amp; ids.length > 0">
        and RES.ID_ in
        <foreach item="item" index="index" collection="ids"
                 open="(" separator="," close=")">
          #{item}
        </foreach>
      </if>
      <if test="name != null">
        and RES.NAME_ = #{name}
      </if>
      <if test="nameLike != null">
        and RES.NAME_ like #{nameLike} ESCAPE ${escapeChar}
      </if>
      <if test="type != null">
        and RES.TYPE_ = #{type}
      </if>
      <if test="userId != null">
        and U.ID_ = #{userId}
      </if>
      <if test="procDefId != null">
        and exists (select ID_ from ${prefix} ACT_RU_IDENTITYLINK where PROC_DEF_ID_ = #{procDefId} and GROUP_ID_=RES.ID_ )
      </if>
      <if test="tenantId != null">
        and TM.TENANT_ID_ = #{tenantId}
      </if>
      
      <include refid="org.camunda.bpm.engine.impl.persistence.entity.AuthorizationEntity.queryAuthorizationCheck" />
    </where>
  </sql> 
  
</mapper>
